//
//  BaseTabBarController.swift
//  StoreProject
//
//  Created by CYC on 1998/12/29.
//

import UIKit

class BaseTabBarController: UITabBarController {
    
    var currentIndex: NSInteger?    // 当前选择

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.currentIndex = 0
        self.tabBar.backgroundColor = .white
        
        self.addVCAction(MainVC(), name: "今天".languageText, normalImage: "tab1n", selectImage: "tab1s")

        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : kMainColor], for: .selected)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : kLabelColor3], for: .normal)
        self.tabBar.tintColor = kMainColor  // 解决：UITabbar一开始颜色正确，进入页面再出来后颜色错误的问题
        
        
        self.tabBar.isTranslucent = true
        self.tabBar.isHidden = true

        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // 修正标签栏高度
        kTabBarHeight = self.tabBar.frame.size.height
    }

    
    // MARK:添加控制器
    @objc func addVCAction(_ ctrl: UIViewController, name: String, normalImage: String, selectImage: String) {
        
        ctrl.tabBarItem.title = name
//        ctrl.tabBarItem.image = UIImage.init(named: normalImage)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
//        ctrl.tabBarItem.selectedImage = UIImage.init(named: selectImage)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        self.addChild(BaseNavigationController.init(rootViewController: ctrl))
    }

    // MARK:点击
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        if let index = tabBar.items?.firstIndex(of: item) {
            
            if index != currentIndex {
                
                animalAction(index)
            }
        }
        
    }

    // MARK:动画
    @objc func animalAction(_ index: NSInteger) {
        
//        var viewList = [UIView]()
//        for tabbarButton in tabBar.subviews {
//
//            if tabbarButton.isKind(of: NSClassFromString("UITabBarButton")!) {
//
//                viewList.append(tabbarButton)
//            }
//        }
//
//        let animal = CABasicAnimation.init(keyPath: "transform.scale")
//        animal.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeInEaseOut)
//        animal.duration = 0.1
//        animal.repeatCount = 1
//        animal.autoreverses = true
//        animal.fromValue = 0.7
//        animal.toValue = 1.1
//
//        let tabbarButton = viewList[index]
//        tabbarButton.layer.add(animal, forKey: nil)
        
        currentIndex = index
    }
    
    
}
